package com.beidouapp.security.mvc;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beidouapp.common.util.DateUtils;
import com.beidouapp.domain.pojo.AuditLog;
import com.beidouapp.exception.BusinessException;
import com.beidouapp.security.model.ReturnDataInfo;
import com.beidouapp.security.model.SessionUser;
import com.beidouapp.service.AuditLogService;
import com.beidouapp.utils.UIUtils;

import net.sf.json.JSONObject;

/**
 * 日志管理
 * @author TieWei
 *
 */
@Controller
@RequestMapping(value = "/audit")
public class AuditLogContorller
{

	@Autowired
	private AuditLogService auditLogService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody ReturnDataInfo<AuditLog> findAuditLogList(javax.servlet.http.HttpSession session, HttpServletRequest request,
			@RequestParam(value = "iDisplayStart", required = false, defaultValue = "0") Integer offset, 
			@RequestParam(value = "iDisplayLength", required = false, defaultValue = "10") Integer limit, 
			@RequestParam(value = "globalSearch", required = false, defaultValue = "{}") String globalSearch)
	{
		try
		{
			Map paramters = new HashMap();
			if (globalSearch != null)
			{
				JSONObject jsonObject = JSONObject.fromObject(globalSearch);
				paramters.putAll(jsonObject);
			}
			paramters.putAll(request.getParameterMap());
			List<AuditLog> result = new ArrayList<AuditLog>();
	
			if (paramters.get("beginDate") == null)
			{
				paramters.put("beginDate", DateUtils.getDateWithStart(new Date()));
			}
			else
			{
				paramters.put("beginDate", DateUtils.getDateWithStart(DateUtils.parse(paramters.get("beginDate").toString(), DateUtils.FORMAT_SHORT)));
			}
			
			if (paramters.get("endDate") == null)
			{
				paramters.put("endDate", DateUtils.getDateWithEnd(new Date()));
			}
			else
			{
				paramters.put("endDate", DateUtils.getDateWithEnd(DateUtils.parse(paramters.get("endDate").toString(), DateUtils.FORMAT_SHORT)));
			}
			SessionUser sessionUser = UIUtils.getSessionUser();
			if(sessionUser == null)
			{
				throw new BusinessException("session信息不存在");
			}
			paramters.put("orgCode", sessionUser.getOrgCode());
			
			int totalcount = auditLogService.countByExample(paramters);
			result = auditLogService.selectByExample(paramters, offset, limit);
			return ReturnDataInfo
					.createSuccessfulExecuteResults(result, offset, limit, Long.valueOf(totalcount));
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return ReturnDataInfo.createFailedExecuteResult(ex.getMessage());
		}
	}

}
